<?php

namespace app\backend\service;

use app\common\model\SourceModel;
use app\common\RootService;
use app\common\validate\SourceValidate;

class SourceService extends RootService
{
    /**
     * 资源标签
     * @return array
     */
    public function sourceLabel()
    {
        return ['stateLabel' => SourceModel::$stateLabel, 'adminNames' => AdminService::adminNames()];
    }

    /**
     * 来源列表
     * @param array $params
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function lists(array $params)
    {
        // 资源标签
        $sourceLabel = $this->sourceLabel();
        // 数据源
        $sourceList = $this->page($this->SourceModel($params)->order(['id' => 'desc']));
        // 数据处理
        array_walk($sourceList['data'], function (&$row) use ($sourceLabel) {
            empty($row['remark']) && $row['remark'] = '--';
            $row['stateLabel'] = $sourceLabel['stateLabel'][$row['state']] ?? '--';
            $row['realName'] = $sourceLabel['adminNames'][$row['admin_id']]['real_name'] ?? '--';
        });
        return $sourceList;
    }

    /**
     * 添加来源
     * @param array $params
     * @return string[]
     * @throws \app\common\RootException
     */
    public function add(array $params)
    {
        // 参数校验
        $this->validate(new SourceValidate(), 'add');
        // 添加失败
        $addData = ['admin_id' => adminInfo('id'), 'name' => $params['name'], 'domain' => $params['domain'],
            'state' => $params['state'], 'remark' => $params['remark']];
        SourceModel::create($addData) || $this->errorException('添加失败');
        // 添加成功
        return ['msg' => '添加成功', 'preload' => true];
    }

    /**
     * 修改来源
     * @param array $params
     * @return string[]
     * @throws \app\common\RootException
     */
    public function edit(array $params)
    {
        // 参数校验
        $this->validate(new SourceValidate(), 'edit');
        // 编辑失败
        $renewData = ['id' => $params['id'], 'name' => $params['name'], 'domain' => $params['domain'],
            'state' => $params['state'], 'remark' => $params['remark']];
        SourceModel::update($renewData) || $this->errorException('修改失败');
        // 添加成功
        return ['msg' => '修改成功', 'preload' => true];
    }


    /**
     * 来源详情
     * @param int $sourceId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(int $sourceId)
    {
        return SourceModel::where(['id' => $sourceId])->find()->toArray();
    }

    /**
     * 删除来源
     * @param int $sourceId
     * @return array
     * @throws \app\common\RootException
     */
    public function del(int $sourceId)
    {
        // 删除失败
        SourceModel::where(['id' => $sourceId])->delete() || $this->errorException('删除失败');
        // 删除成功
        return ['msg' => '删除成功', 'preload' => true];
    }

    /**
     * 来源模型
     * @param array $params
     * @return SourceModel
     */
    protected function SourceModel(array $params)
    {
        // 来源名称筛选
        empty($params['name']) || $where[] = ['name', '=', $params['name']];
        // 来源状态筛选
        condition($params['state'] ?? '') && $where[] = ['state', 'in', explode(',', $params['state'])];
        // 筛选结果
        return SourceModel::where($where ?? []);
    }
}